<template>
  <div>
    <el-table :data="tableData" style="width: 100%" row-key="id">
      <el-table-column prop="title" label="权限名称" width="180" />
      <el-table-column label="图标" width="180">
        <template #default="scope">
          <div style="display: flex; align-items: center">
            <el-icon size="20px">
              <component :is="mapIcons[scope.row.icon]"></component>
            </el-icon>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button type="primary" round @click="handleUpdate(scope.row)">更新</el-button>
          <el-popconfirm title="你确定要删除吗？" @confirm="handleDelete(scope.row)" confirm-button-text="确定"
            cancel-button-text="取消">
            <template #reference>
              <el-button type="danger" round>删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model="dialogVisible" title="更新权限" width="500">
      <span>
        <el-form ref="updateFormRef" style="max-width: 600px" :model="updateForm" :rules="rules" label-width="auto"
          class="demo-ruleForm" status-icon>
          <el-form-item label="权限名称" prop="title">
            <el-input v-model="updateForm.title" />
          </el-form-item>
        </el-form>
      </span>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleConfirm()">
            更新
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import axios from 'axios'
import {
  User,
  HomeFilled,
  Key,
  OfficeBuilding,
  UploadFilled,
  List
} from '@element-plus/icons-vue'

const tableData = ref([])

onMounted(() => getList())

const getList = async () => {
  const res = await axios.get("/adminapi/rights")
  tableData.value = res.data
}



// 图标映射文件
const mapIcons = {
  User,
  HomeFilled,
  Key,
  OfficeBuilding,
  UploadFilled,
  List
}

// 表单校验
const dialogVisible = ref(false)
const updateFormRef = ref()
const currentItem = ref({})
const updateForm = reactive({
  title: "",
})
// 表单校验
const rules = reactive({
  title: [
    { required: true, message: '请输入权限名称', trigger: 'blur' }
  ],
})

// 更新对话框

const handleUpdate = (item) => {
  // console.log(item);
  currentItem.value = item
  updateForm.title = item.title
  dialogVisible.value = true
}

const handleConfirm = () => {
  updateFormRef.value.validate(async (valid, fields) => {
    if (valid) {
      await axios.put(`/adminapi/rights/${currentItem.value.id}`, updateForm)
      dialogVisible.value = false

      await getList()
    } else {
      console.log('error submit!', fields)
    }
  })

}


// 删除对话框
const handleDelete = async ({ id }) => {
  await axios.delete(`/adminapi/rights/${id}`)
  console.log(id);

  await getList()
}



</script>